<template>
    <div class="box">
        <div>
            <h1>管理员登录</h1>
        </div>
        <el-form
            ref="ruleFormRef"
            style="max-width: 600px"
            :model="ruleForm"
            :rules="rules"
            label-width="auto"
        >
            <el-form-item label="登录账号" prop="userName">
                <el-input v-model="ruleForm.userName" />
            </el-form-item>
            <el-form-item label="登录密码" prop="userPass">
                <el-input v-model="ruleForm.userPass" show-password />
            </el-form-item>
            
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    登录
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { adminLogin } from '@/api/api'
import { useRouter } from 'vue-router';


const router = useRouter();

interface RuleForm {
    userName: string;
    userPass: string;
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
    userName: "",
    userPass: "",
});

const locationOptions = ["Home", "Company", "School"];

const rules = reactive<FormRules<RuleForm>>({
    userName: [
        {
            required: true,
            message: "请输入登录账号",
            trigger: "blur",
        },
        { min: 3, max: 16, message: "用户名的长度必须3到16个字符", trigger: "blur" },
    ],
    userPass: [
        {
            required: true,
            message: "请输入登录密码",
            trigger: "blur",
        },
        { pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{6,}$/, message: "密码必须包含字母和数字,最小长度是6位", trigger: "blur" },
    ],
    
});

// 用户登录
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validate((valid, fields) => {
        if (valid) {
            // 表单校验通过了
            adminLogin(ruleForm).then(res => {
                if(res.data.code == 200) {
                    // 消息提示
                    ElMessage.success('用户登录成功了');
                    // 保存token和用户信息
                    let user = res.data.data;
                    let token = user.token;
                    window.sessionStorage.setItem('token', token);
                    window.sessionStorage.setItem('user', JSON.stringify(user));

                    // 跳转页面
                    router.push('/home');
                }
                else {
                    ElMessage.error(res.data.msg);
                }
            })
        } else {
            // 表单校验没通过
            console.log("error submit!", fields);
        }
    });
};

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}));
</script>
